from flask import Flask, request, render_template, url_for, jsonify
from bs4 import BeautifulSoup
import requests

url = 'https://www.oschina.net/blog'
heard={'User-Agent':'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}

url1 = 'https://www.oschina.net/action/ajax/'

def init_spider(classification = 0, is_first = 0):
    values = {'classification':classification}
    r = requests.post(url, headers=heard, params=values)
    soup = BeautifulSoup(r.text, 'html.parser')
    top0 = soup.find(id="topsOfRecommend")
    top1 = soup.find(id="topsOfToday")
    top2 = soup.find(id="topsOfWeek")
    top3 = soup.find(id="topsOfDaily")
    top4 = soup.find(id="topsOfNew")
    result = [str(top0), str(top1), str(top2), str(top3), str(top4)]
    if is_first == '1':
        ul = soup.find("ul", class_="blog-nav")
        i = 0
        links = []
        for link in ul.find_all('a'):
            links.insert(i, link.get('href'))
            i += 1
        result.insert(5, links)
    return result

def page_spider(classification, page_number, value):
    if value == '0':
        url2 = url1 + 'get_more_recommend_blog'
    elif value == '3':
        url2 = url1 + 'get_more_daily_blog'
    elif value == '4':
        url2 = url1 + 'get_more_recent_blog'
    else:
        return
    values = {'classification':classification, 'p':page_number}
    r = requests.post(url2, params=values, headers=heard)
    soup = BeautifulSoup(r.text, 'html.parser')
    return str(soup)

if __name__ == '__main__':
    page_spider(0,2,0)